﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using MvvmTutorials.Messenger.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MvvmTutorials.Messenger.ViewModels
{
    public partial class StudentListViewModel
        : ObservableRecipient,
        IRecipient<ValueChangedMessage<Student>>
    {
        public ObservableCollection<Student> Students { get; } = new();

        [ObservableProperty]
        private bool allowAddNew;

        partial void OnAllowAddNewChanged(bool value)
        {
            WeakReferenceMessenger.Default.Send(new ValueChangedMessage<bool>(value));
        }

        public int StudentCount => Students?.Count ?? 0;

        public StudentListViewModel()
        {
            Students.CollectionChanged += (_, _) => OnPropertyChanged(nameof(StudentCount));
        }

        public void Receive(ValueChangedMessage<Student> message)
        {
            Students.Add(message.Value);
        }
    }
}
